import { useRouter } from 'vue-router'
import { workInfos } from '@/stores/work'
import { userInfos } from '@/stores/user'
import pinia from '@/stores/index'
import { message } from 'ant-design-vue'

function useCreateDesign() {
  const workStore = workInfos()
  const userStore = userInfos(pinia)
  const router = useRouter()
  const createDesign = async () => {
    if (userStore.isLogin) {
      const payload = {
        title: '未命名作品',
        desc: '未命名作品',
        content: { components: [], props: {} as PageProps, setting: {} },
        coverImg:
          'http://typescript-vue.oss-cn-beijing.aliyuncs.com/vue-marker/5f81cca3f3bf7a0e1ebaf885.png'
      }

      try {
        const nwork = await workStore.createWork(payload)
        if (nwork) {
          router.push(`/work/editor/${nwork.id}`)
        }
      } catch (error) {
        message.error('创建新的设计失败！')
      }
    } else {
      router.push('/login')
    }
  }
  return createDesign
}

export default useCreateDesign
